//import {request} from 'umi'


const request = async (uri, payload) => {
    return await window.XRemote.invoke(uri, payload.params || payload.data);
}



export const getPages = async () => {
    return await request('/api/getPages', {
        method: 'GET',
        params: {}
    }).then(res => {
        return {
            pages: res.data.pages,
            activeWindowId: res.data.activeWindowId
        }
    });

}

export const newPage = async () => {
    return await request('/api/newPage', {
        method: 'GET',
        params: {}
    }).then(res => {
        return {
            windowId: res.windowId
        }
    });
}

export const changePage = async (payload) => {
    return await request('/api/changePage', {
        method: 'POST',
        data: payload
    }).then(res => {
        return {
            activeWindowId: res.activeWindowId
        }
    });
}

export const closePage = async (payload) => {
    return await request('/api/closePage', {
        method: 'POST',
        data: payload
    }).then(res => {
        return null;
    }); 
}

export const getWindowState = async (payload) => {
    return await request('/api/getWindowState', {
        method: 'GET'
    }).then(res => {
        return {
            windowState: res.windowState
        }
    });
}

export const quit = async (payload) => {
    return await request('/api/quit', {
        method: 'GET'
    }).then(res => {
        return null;
    });
}


export const minimize = async (payload) => {
    return await request('/api/minimize', {
        method: 'GET'
    }).then(res => {
        return {
            windowState: res.windowState
        }
    });
}

export const maximize = async (payload) => {
    return await request('/api/maximize', {
        method: 'GET'
    }).then(res => {
        return {
            windowState: res.windowState
        }
    });
}

export const openUrl = async (payload) => {
    return await request('/api/openUrl', {
        method: 'POST',
        data: payload
    }).then(res => {
        return {
            windowId :res.windowId,
            title : res.title,
            url :res.url
        }
    });
}

export const openDev = async (payload) => {
    return await request('/api/openDev', {
        method: 'GET'
    }).then(res => {
        return null;
    });
}

export const capture = async (payload) => {
    return await request('/api/capture', {
        method: 'GET'
    }).then(res => {
        return null;
    });
}

export const reload = async (payload) => {
    return await request('/api/reload', {
        method: 'GET'
    }).then(res => {
        return null;
    });
}

export const navigate = async (payload) => {
    console.log(payload);
    return await request('/api/'+payload.role, {
        method: 'GET'
    }).then(res => {
        return null;
    });
}